import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/server/api/expert_api.dart';
import 'package:ball_lion/utils/date_time_utils.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/utils/widget_utils.dart';
import 'package:visibility_detector/visibility_detector.dart';
import 'package:ball_lion/entity/home/subs_list_entity.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';

class SubsBar extends StatefulWidget {
  const SubsBar({super.key, required this.expertId, this.padding});

  final String expertId;
  final EdgeInsets? padding;

  @override
  State<SubsBar> createState() => _SubsBarState();
}

class _SubsBarState extends State<SubsBar> {
  SubsListEntity? data;
  int? roomId = Get.parameters['roomId'].toInt();

  @override
  void initState() {
    getData();
    super.initState();
  }

  Future getData() async {
    data = await ExpertApi.getSubsDetail(widget.expertId, roomId: roomId);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    if (data == null) {
      return Container();
    } else {
      return VisibilityDetector(
        key: const Key("subsBar"),
        onVisibilityChanged: (info) {
          if (!info.visibleBounds.isEmpty) {
            getData();
          }
        },
        child: Container(
            clipBehavior: Clip.antiAlias,
            height: 66,
            padding: const EdgeInsets.symmetric(horizontal: 16),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(8),
                image: DecorationImage(
                    image: AssetImage(Utils.getImgPath("subs_bar_back.png")),
                    isAntiAlias: true,
                    fit: BoxFit.cover)),
            child: Row(
              children: [
                Expanded(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Flexible(
                        child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              QsText(
                                  "专家订阅卡 ${data?.subsPrices?.first.needPrices}球币/${data?.subsPrices?.first.readNum}篇",
                                  fontSize: 16,
                                  color: Colours.goldFFEEC8,
                                  fontWeight: FontWeight.w600,
                                  height: 1.6),
                              data?.endTime.hasValue == true
                                  ? QsText(
                                      "有效期：${DateTime.parse(data?.endTime ?? "").formatedString("yyyy-MM-dd")}",
                                      fontSize: 10,
                                      color: const Color(0xFFC7BA9E),
                                      height: 1.4)
                                  : QsText(
                                      "购买订阅，低至${data?.subsPrices?.first.desc}",
                                      maxLines: 2,
                                      color: Colours.goldD6A864,
                                      fontSize: 12,
                                      height: 1.2),
                            ]),
                      ),
                    ],
                  ),
                ),
                // Container(width: 4),
                Container(
                  width: 72,
                  height: 32,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      gradient: const LinearGradient(
                          colors: [Color(0xFFFFE1A0), Color(0xFFFFB443)],
                          begin: Alignment.centerLeft,
                          end: Alignment.centerRight),
                      borderRadius: BorderRadius.circular(30)),
                  child: const QsText(
                    "立即订阅",
                    fontSize: 13,
                    fontWeight: FontWeight.w500,
                    color: Colours.brown493B31,
                  ),
                ).tap(() {
                  if (Get.currentRoute.contains(Routes.userPage)) {
                    Utils.onEvent('expert_detail_subscribe');
                  } else if (Get.currentRoute.contains(Routes.viewpoint)) {
                    Utils.onEvent('plan_detail_expert_subscribe', params: {
                      'expert_id': widget.expertId,
                      'param2': roomId == null ? '0' : '1'
                    });
                  }
                  Get.toNamed(Routes.subsPay,
                      arguments: widget.expertId,
                      parameters: {"roomId": "${roomId ?? ""}"});
                })
              ],
            )),
      );
    }
  }

  Widget textBlock(String title, String content) {
    return Column(
      children: [
        QsText(content, color: Colours.goldFFEEC8),
        QsText(title, fontSize: 10, color: Colours.brownC7BA9E)
      ],
    );
  }
}
